package com.licheng.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.licheng.mapper.StudentMapper;
import com.licheng.pojo.Student;
import com.licheng.pojo.StudentPageQuery;
import com.licheng.pojo.StudentPageResult;
import com.licheng.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 条件分页查询学生
     * @param spq
     * @return
     */
    @Override
    public StudentPageResult selctStudentPage(StudentPageQuery spq) {
        //调用pageHader
        PageHelper.startPage(spq.getPage(), spq.getPageSize());

        List<Student> studentList = studentMapper.selctStudentPage(spq);
        Page p = (Page) studentList;

        long total = p.getTotal();
        List<Student> rows = p.getResult();
        return new StudentPageResult(total, rows);
    }

    /**
     * 根据id批量删除学生
     * @param ids
     */
    @Override
    public void deleteStudentById(List<Integer> ids) {
        studentMapper.deleteStudentById(ids);
    }

    /**
     * 添加学生
     * @param student
     */
    @Override
    public void insertStudent(Student student) {
        //添加基本数据
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);
    }

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    @Override
    public Student selectStudentById(Integer id) {
        Student student = studentMapper.selectStudentById(id);
        return student;
    }

    /**
     * 修改学生
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    /**
     * 违纪
     * @param id 学生id
     * @param score 违纪扣分
     */

    @Override
    public void violation(Integer id, Integer score) {
        //扣分成功就添加次数
        studentMapper.violationScoreAndCount(id, score);
    }


}
